<template>
  <div>
    <yd-navbar title="选择出行人员">
        <router-link :to="{path:'boutiquelinestep1',query: {'id': product_id, 'manNum': g_manNum, 'childNum': g_childNum, 'datetime': datetime, 'g_totalMoney': g_totalMoney}}" slot="left">
            <yd-navbar-back-icon></yd-navbar-back-icon>
        </router-link>
        <div slot="right" @click="save()" ref="mybox">完成</div>
    </yd-navbar>
    <div class="mui-content">
        <yd-cell-item>
            <yd-icon slot="icon" name="order" size=".42rem" @click.native="adduser()"></yd-icon>
            <span slot="left">添加人员</span>
        </yd-cell-item>
        <yd-search v-model="value1" :on-submit="submitHandler"></yd-search>
        <div class="usercontent" :change="checkuser()">
          <yd-checkbox-group v-model="checkbox">
              <yd-checkbox :val="JSON.stringify(tourlist)" shape="circle" class="userlist" v-for="(tourlist, index) in tourist" :key="tourlist.index">{{tourlist.nickname}}</yd-checkbox>
          </yd-checkbox-group>
        </div>
		</div>
	</div>
</template>

<script>
import database from 'hdc/database';
export default {
  name: 'boutiquelinefriend',
  components: {
    database
  },
  data() {
    return {
      value1: '',
      list: [
        {'label': '哈哈哈'},
        {'label': '嘿嘿黑'}
      ],
      checkbox: [],
      tourist: [],
      backTourist: [],
      manNum: '', // 成人数量
      childNum: '', // 儿童数量
      product_id: this.$route.query.id, // 产品id
      g_manNum: this.$route.query.g_manNum, // 可添加成人数量成人数量
      g_childNum: this.$route.query.g_childNum, // 可添加儿童数量
      datetime: this.$route.query.datetime, // 时间
      g_totalMoney: this.$route.query.g_totalMoney // 总价
    }
  },
  mounted() {
    this.load();
    this.getFriendList();
    console.log(this.$route.query.g_manNum, this.$route.query.g_childNum, this.$route.query.datetime, this.$route.query.g_totalMoney);
  },
  methods: {
    load() {
    },
    submitHandler(value) {
      this.$dialog.toast({mes: `搜索：${value}`});
    },
    getFriendList() {
      // 获取添加的好友列表
      this.hdAjax({
        url: this.API.friendlist,
        success: resultData => {
          console.log('好友列表');
          console.log(resultData.result);
          for (const key in resultData.result) {
            if (resultData.result.hasOwnProperty(key)) {
              const element = resultData.result[key];
              element.forEach(list => {
                this.tourist.push(list);
                console.log(this.tourist);
              });
            }
          }
        }
      });
    },
    // 点击checkbox选择出行人
    checkuser(tour) {
      console.log('=======');
      console.log(this.checkbox);
      this.backTourist.length = 0;
      for (var i = 0; i < this.checkbox.length; i++) {
        console.log('------------0---');
        var list = JSON.parse(this.checkbox[i]);
        this.backTourist[i] = list;
        this.discriCard(list.cardid);
        this.backTourist[i]['age'] = this.discriCard(list.cardid);
      }
      console.log(this.backTourist);
    },
    // 点击完成把选择好的出行人传回到下单页
    save() {
      console.log(this.backTourist);
      var manNum = 0;
      var childNum = 0;
      console.log('可选择成人数量和儿童数量' + this.g_manNum, this.g_childNum);
      for (var j = 0; j < this.backTourist.length; j++) {
        if (this.backTourist[j].age > 16) {
          manNum++;
        } else {
          childNum++;
        }
      }
      console.log('chengren----' + manNum);
      console.log('ertong----' + childNum);
      console.log(manNum);
      console.log(this.g_manNum);
      if (manNum !== parseInt(this.g_manNum)) {
        alert('请选择' + this.g_manNum + '位成人');
      } else if (childNum !== parseInt(this.g_childNum)) {
        alert('请选择' + this.g_childNum + '位儿童');
      } else {
        this.$router.push({path: 'boutiquelinestep1', query: {'id': this.product_id, 'manNum': this.g_manNum, 'childNum': this.g_childNum, 'datetime': this.datetime, 'totalMoney': this.g_totalMoney, 'tourist': this.backTourist}});
      }
    },
    // 根据身份证号码获取性别和出生日期
    discriCard(UUserCard) {
      // 获取输入身份证号码
      // var UUserCard = '';
      // 获取出生日期
      // var 添加人员 = UUserCard.substring(6, 10) + '-' + UUserCard.substring(10, 12) + '-' + UUserCard.substring(12, 14);
      // console.log(添加人员);
      // // 获取性别
      // if (parseInt(UUserCard.substr(16, 1)) % 2 === 1) {
      //   console.log('男');
      //   // 是男则执行代码
      // } else {
      //   console.log('女');
      //   // 是女则执行代码
      // }
      // 获取年龄
      var myDate = new Date();
      var month = myDate.getMonth() + 1;
      var day = myDate.getDate();
      var age = myDate.getFullYear() - UUserCard.substring(6, 10) - 1;
      if (UUserCard.substring(10, 12) < month || UUserCard.substring(10, 12) === month && UUserCard.substring(12, 14) <= day) {
        age++;
      }
      console.log(age);
      return age;
      // 年龄age
    },
    // 点击图标进行添加人员
    adduser() {
      this.$router.push({path: 'boutique_adduser', query: {'id': this.product_id, 'g_manNum': this.g_manNum, 'g_childNum': this.g_childNum, 'datetime': this.datetime, 'g_totalMoney': this.g_totalMoney}});
    }
  }
}
</script>

<style>
body{
  background-color: #ffffff;
}
.userlist {
  height: 40px;
  padding: 10px 20px;
  border-bottom:  1px solid #e8e8e8;
  size: 20px;
  width:100%;
}
.userhead {
  height: 25px;
  padding-left: 10px;
  color: #cfcfcc;
  font-size: 20px;
  font-weight: bold;
  background-color: #f2f2f2;
  border-bottom:  1px solid #e8e8e8;
}
.mui-ios .mui-indexed-list-inner {
  width: calc(100% + 10px);
}
.mui-indexed-list-inner {
    margin: 0px;
    padding: 0px;
    overflow-y: auto;
    border: none;
}
.mui-indexed-list-alert.active {
    display: block;
}
.mui-indexed-list-alert {
    position: absolute;
    z-index: 20;
    background-color: rgba(0, 0, 0, 0.5);
    width: 80px;
    height: 80px;
    left: 50%;
    top: 50%;
    margin-left: -40px;
    margin-top: -40px;
    border-radius: 40px;
    text-align: center;
    line-height: 80px;
    font-size: 35px;
    color: #fff;
    display: none;
    -webkit-transition: .2s;
}

</style>
